/**
 * 简单的语法检查脚本
 * 检查关键的ArkTS语法问题
 */

const fs = require('fs');
const path = require('path');

// 检查的文件列表
const filesToCheck = [
  'entry/src/main/ets/services/SecureApiKeyService.ets',
  'entry/src/main/ets/services/VolcengineTextToSpeechService.ets',
  'entry/src/main/ets/services/DeepSeekService.ets'
];

console.log('开始语法检查...\n');

let hasErrors = false;

filesToCheck.forEach(filePath => {
  console.log(`检查文件: ${filePath}`);
  
  try {
    const content = fs.readFileSync(filePath, 'utf8');
    const lines = content.split('\n');
    
    // 检查常见的ArkTS语法问题
    lines.forEach((line, index) => {
      const lineNum = index + 1;
      
      // 检查对象字面量作为类型
      if (line.includes('as {') && !line.includes('as TTSAppInfo') && !line.includes('as TTSUserInfo') && !line.includes('as TTSAudioConfig') && !line.includes('as TTSRequestInfo')) {
        console.log(`  ❌ 第${lineNum}行: 可能的对象字面量类型问题`);
        hasErrors = true;
      }
      
      // 检查for...in循环
      if (line.includes('for (') && line.includes(' in ')) {
        console.log(`  ❌ 第${lineNum}行: for...in循环不被支持`);
        hasErrors = true;
      }
      
      // 检查any类型
      if (line.includes(': any') || line.includes('<any>')) {
        console.log(`  ❌ 第${lineNum}行: any类型不被支持`);
        hasErrors = true;
      }
      
      // 检查Promise访问属性
      if (line.includes('Promise<') && (line.includes('.appId') || line.includes('.accessToken') || line.includes('.cluster'))) {
        console.log(`  ❌ 第${lineNum}行: 直接访问Promise对象属性`);
        hasErrors = true;
      }
      
      // 检查catch回调参数类型
      if (line.includes('.catch(') && line.includes('error =>') && !line.includes('(error: Error)')) {
        console.log(`  ❌ 第${lineNum}行: catch回调参数缺少类型声明`);
        hasErrors = true;
      }
    });
    
    console.log(`  ✅ ${filePath} 检查完成\n`);
    
  } catch (error) {
    console.log(`  ❌ 无法读取文件: ${error.message}\n`);
    hasErrors = true;
  }
});

if (hasErrors) {
  console.log('❌ 发现语法问题，需要修复');
  process.exit(1);
} else {
  console.log('✅ 所有检查通过，语法看起来正确！');
  process.exit(0);
}